/**
 * *****************************************************************
 * @file    adt3102_timer.h
 * @author  WuHao(hwu@andartechs.com.cn)
 * @version 1.0.0
 * @date    2020-11-24
 * @brief   1.1.0, 2021/10/04, Jack, add app timer
 *                 Copyright (c) 2020, Andar Technologies Inc.
 *                           www.andartechs.com 
 *
 * *****************************************************************
 */
#ifndef __Timer_H
#define __Timer_H
/*------------------------------ include -------------------------------------*/
#include "adt3102_type_define.h"
#include "timer_ctype_map.h"


/*------------------------------ define --------------------------------------*/
#define clkDIV1   00
#define clkDIV16  01
#define clkDIV256 10

#define MAX_APP_TIMER_NUM 12

typedef void (*timerCb)(void);
/*------------------------------ typedef -------------------------------------*/
typedef struct {
    int8  id;
    int8  active;
    uint8 start;
    int32 counter;
    timerCb  timer_cb;
}ADT_APP_Timer_Typedef;

extern volatile uint32 g_app_timerout;

/*------------------------------ function ------------------------------------*/
/**
 * @brief   Timer initialization, 50:50M high clock,if switch 125M high clock, 
 *          we have to change 50 to 125.
 * @param   time: set the time of the timer,the unit is us.
 * @param   Timerx: include Timer0,Timer1.
 * @param   highClock50M: select high clock, include 50M and 125M.
 * @return  There is a fixed 1us for timer's function call and interruption call. for example,
 *            : when time=1   (us), the actual period is about 2us.
 *            : when time=10  (us), the actual period is about 11us.
 *            : when time=100 (us), the actual period is about 101us.
 *            : when time=1000(us), the actual period is about 1001us..
 * @note
 */
typedef void (*timerCallback)(void *arg);
extern timerCallback timer0Start(float time, timerCallback timerFunc,void *data,int32 highClock50M);
void timerInit(TimerTypeDef *Timerx, float time,int32 highClock50M);

/**
 * @brief   cloes timer,clear current value.
 * @param   Timerx: include Timer0,Timer1.
 * @return  None.
 * @note
 */
void timerOff(TimerTypeDef *Timerx);

/**
 * @brief   Timer external clock input for GPIO.
 * @param   time: set the time of the timer,the unit is us.
 * @param   Timerx: include Timer0,Timer1.
 * @param   highClock50M: select high clock, include 50M and 125M.
 * @return  None.
 * @note
 */
void timerInclock(TimerTypeDef *Timerx ,float time, int32 highClock50M);

/**
 * @brief   Timer external clock enable for GPIO.
 * @param   time: set the time of the timer,the unit is us.
 * @param   Timerx: include Timer0,Timer1.
 * @param   highClock50M: select high clock, include 50M and 125M.
 * @return  None.
 * @note
 */
void timerInenable(TimerTypeDef *Timerx ,float time,int32 highClock50M);
void timerCallbackTest(int32 *a);

/**
 * @brief   dualtimerInit.
 * @param   reloadValue: timer reload.
 * @param   clkDiv: clock is divided by clkDiv.
 *          clkDIV1   :divided by 1
 *          clkDIV16  :divided by 16
 *          clkDIV256 :divided by 256
 * @return  None.
 * @note
 */
void dualtimerInit(uint32 reloadValue, int32 clkDiv);

/**
 * @brief   APP Timer initialization, 50:50M high clock,
 *          if switch 125M high clock, we have to change 50 to 125
 * @param   Timerx: include Timer0,Timer1.
 * @param   highClock50M: set Timer clock.
 * @return  None.
 * @note
 */
void appTimerInit(TimerTypeDef *Timerx, int32 highClock50M);

/**
 * @brief   Allocate a new APP Timer.
 * @param   None.
 * @return  id.
 * @note
 */
int8 appTimerNew(timerCb func);

/**
 * @brief   Allocate a new APP Timer.
 * @param   id: APP Timer id.
 * @param   counter: APP Timer counter.
 * @return  None.
 * @note
 */
void appTimerStart(int8 id, int32 counter);

/**
 * @brief   stop a APP Timer.
 * @param   id: APP Timer id.
 * @return  None.
 * @note
 */
void appTimerStop(int8 id);

/**
 * @brief   clean all APP Timer.
 * @param   None.
 * @return  None.
 * @note
 */
void appTimerClean(void);

/**
 * @brief   update all active APP Timer.
 * @param   None.
 * @return  None.
 * @note
 */
void appTimerUpdate(void);

/****************************************************************************************
*
*Function name : app_timerAdjust
*Description   : fastforward all active APP Timer by value in million second
*Paramater		 :
*                int32 value

*Return        : None
****************************************************************************************/
void app_timerAdjust(int32 value);
/****************************************************************************************
*
*Function name : appTimerCheckTimeout
*Description   : check if timeout happened with one certain APP Timer
*Paramater		 :
*                int8 id : APP Timer id
*Return        : int32 : timeout status
****************************************************************************************/
int32 appTimerCheckTimeout(int8 id);

#endif
